package com.ck.ck_back.controller;

import com.ck.ck_back.pojo.Production;
import com.ck.ck_back.pojo.Result;
import com.ck.ck_back.service.productionservice;

import com.ck.ck_back.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production")
public class ProductionController {

    @Resource
    private productionservice productionserviceimpl;

    public static  int i=1;
    public static int x=1;

    @PostMapping("/getbyid")
    public Object get(int production_id){
        Object obj = productionserviceimpl.select(production_id);
        if (obj !=null){
            return Result.success(productionserviceimpl.select(production_id));
        }else {
            return Result.failure("无此作品");
        }
    }

    @PostMapping("/selectbytype")
    public Object selectbytype(int type,int num,int state,boolean refresh){
        List<Production> all =productionserviceimpl.selectbytype(type,state);
        int pagenum = all.size()/num+(all.size()%num !=0 ?1:0);
//        当查询到结果的最后一个数据（num-1）的id要和传入的相同的话，那么即刷新
//        不然即是加载更多的数据
        if (all.size()>num){
//            刷新
            if (refresh){
                i=1;
                PageHelper.startPage(i,num);
            }else {
//                数据加载
                i++;
                PageHelper.startPage(i,num);
            }
        }else {
            i=1;
            PageHelper.startPage(i,all.size());
        }
        List<Production> obj =productionserviceimpl.selectbytype(type,state);
        Map map = new HashMap();
        map.put("data",obj);
        map.put("pagenum",pagenum);
        map.put("length",all.size());
        if (obj != null){
            return Result.success(map);
        }else {
            return Result.failure("参数错误/此类尚没有数据");
        }
    }

    //    插入内容,404参数错误，2失败,不然成功并返回id
    @PostMapping("/insert")
    public Object insertSelective(Production production){
        int result = productionserviceimpl.insertSelective(production);
        if (result == 40400){
            return Result.failure("参数错误");
        }else if (result == 20000){
            return Result.failure("插入失败");
        }else {
            return Result.success(result);
        }
    }

//    修改内容,404参数错误,3无此作品，先创建,2更新失败,1成功
////    内容id和文章绑定死，不允许更改
    @PostMapping("/update")
    public Object update(Production production){
        int result = productionserviceimpl.update(production);
        if (result == 1){
            return Result.success(productionserviceimpl.select(production.getProductionId()));
        }else if (result == 2){
            return  Result.failure("更新失败");
        }else if(result == 3){
            return  Result.failure("无此作品，请先创建");
        }else {return Result.failure("参数错误");}
    }


//    查询作者不同状态的作品
    @PostMapping("/getproduction_bystate")
    public Object selectby_openid_state(String openid, int state){
        Object obj=productionserviceimpl.selectby_openid_state(openid,state);

        if (obj != null){
            return Result.success(obj);
        }else {
            return Result.failure("无数据");
        }
    }

    //    修改文章的状态,404参数错误，3无此作品，2更新失败，1更新成功
    @PostMapping("/update_state")
    public Object update_state(int production_id,int state){
        int result = productionserviceimpl.update_state(production_id,state);
        if ( result ==1 ){
            return Result.success();
        }else if (result ==2){
            return Result.failure("更新失败");
        }else if (result ==3){
            return Result.failure("无此作品");
        }else {
            return Result.failure("参数失败");
        }
    }

    @PostMapping("getcomment_num")
    public Object getnum(int production_id){
        int num = productionserviceimpl.select_count_comment(production_id);
        if (num != 404000){
            return Result.success(num);
        }else {
            return Result.failure("参数错误");
        }
    }

    @PostMapping("/get_collect_user")
    public Object getuser(int production_id){
        return Result.success(productionserviceimpl.select_collect_user(production_id));
    }

    @PostMapping("/get_focus_production")
    public Object getproduction(String openid,int num ,boolean refresh){
        List<Production> all =productionserviceimpl.select_focused(openid);
        int pagenum = all.size()/num+(all.size()%num !=0 ?1:0);
//        当查询到结果的最后一个数据（num-1）的id要和传入的相同的话，那么即刷新
//        不然即是加载更多的数据
        if (all.size()>num){
//            刷新
            if (refresh){
                x=1;
                PageHelper.startPage(1,2);
            }else {
//                数据加载
                x++;
                PageHelper.startPage(x,num);
            }
        }else {
            x=1;
            PageHelper.startPage(x,all.size());
        }
        System.out.println(x);

        List<Production> obj = PageUtil.startPage(all,x,num);
        System.out.println(obj.size());
        Map map = new HashMap();
        map.put("data",obj);
        map.put("pagenum",pagenum);
        map.put("length",all.size());
        if (all != null){
            return Result.success(map);
        }else {
            return Result.failure("参数错误/此类尚没有数据");
        }
    }

    @PostMapping("/getswiper")
    public Object getswiper(){
        return Result.success(productionserviceimpl.select_swiper());
    }

    @PostMapping("/getaudit")
    public Object getaudit(){
        return Result.success(productionserviceimpl.getaudit());
    }
}
